<?php
class Admin_ArticleClassController extends BaseController {
    protected $layout = 'layouts.admin';
	public function index()
	{
		$article_classes = App::make('ArticleClass')->orderBy('keys', 'ASC')->orderBy('orders', 'ASC')->get();
		$this->layout->content = View::make('admin.article_class.index')->with(compact('article_classes'));
	}
	
	public function create()
	{
		$article_classes = ArticleClass::all();
		$this->layout->content = View::make('admin.article_class.create')->with(compact('article_classes'));
	}
	
	public function store()
	{
			$data = Input::get('data');
			$article_class = new ArticleClass();
			foreach ($data as $k=>$v) {
				$article_class->$k = $v;
			}
			$article_class->level = 0;
			if($article_class->parent_id)
			{
				$parent = AricleClass::find($article_class->parent_id);
				$article_class->level = $parent->level+1;
			}
			$article_class->creator_id = Auth::user()->id;
			$article_class->status = 1;

			if ( $article_class->save() ) {
				return Redirect::route('admin.article_class.index')->withMessage('successfull');				
			}else{
				 // 添加失败
					return Redirect::back()
							->withInput()
							->with('error', '<strong>添加失败。</strong>');
			}	
	}

	public function edit($id)
	{
		$article_classes = ArticleClass::all();
		$article_class= App::make('ArticleClass')->find($id);
		if($article_class)
		{
			$this->layout->content = View::make('admin.article_class.edit')->with(compact('article_class', 'article_classes'));

		} else {
				return Redirect::to("/");
		}
	}
	
	public function update($id)
	{
			$data = Input::get('data');
			$article_class = App::make('ArticleClass')->find($id);
			foreach ($data as $k=>$v) {
				$article_class->$k = $v;
			}

			if ( $article_class->save() ) {
				return Redirect::route('admin.article_class.index')->withMessage('successfull');				
			}else{
				 // 添加失败
					return Redirect::back()
							->withInput()
							->with('error', '<strong>添加失败。</strong>');
			}	
	}
		/**
		 * 资源删除动作
		 * DELETE      /resource/{id}
		 * @param  int  $id
		 * @return Response
		 */
		public function destroy($id)
		{
				$data = App::make('ArticleCategory')->find($id);
				if (is_null($data))
				{
						return Redirect::back()->with('error', '没有找到对应的文章分类。');
				} elseif ($data->delete()) {
						$articles = App::make('Article')->where('article_cat_id', $id);
						$articles->delete();
						return Redirect::back()->with('success', '文章分类删除成功。');
				} else {
						return Redirect::back()->with('warning', '文章分类删除失败。');
				}
		}
}